package com.odobenus.client.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;

import com.odobenus.client.anno.OdobenusItem;
import com.odobenus.client.properties.OdobenusProperties;

public class BeanUtils {
	private static final String BASE_DIR = System.getProperty("catalina.home")+ File.separator+"webapps"+File.separator+"odobenus";
	private static String buildLocalFilePath(OdobenusProperties odobenusProperties, String filePath) {
		File base =  new File(BASE_DIR);
		boolean BASE_EXITS = base.exists();
        if(!BASE_EXITS){
        	base.mkdirs();
        }
		if(!new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()).exists()) {
			new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()).mkdir();
		}if(!new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app").exists()) {
			new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app").mkdir();
		}if(!new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()).exists()) {
			new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()).mkdir();
		}if(!new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()+"/version").exists()) {
			new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()+"/version").mkdir();
		}if(!new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()+"/version"+"/"+odobenusProperties.getVersion()).exists()) {
			new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()+"/version"+"/"+odobenusProperties.getVersion()).mkdir();
		}if(!new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()+"/version"+"/"+odobenusProperties.getVersion()+"/"+odobenusProperties.getEnv()).exists()) {
			new File(BASE_DIR+"/"+odobenusProperties.getLocalDownDir()+"/app/"+odobenusProperties.getApp()+"/version"+"/"+odobenusProperties.getVersion()+"/"+odobenusProperties.getEnv()).mkdir();
		}
		return BASE_DIR.concat(
						odobenusProperties.getLocalDownDir()
		        		.concat("/app/")
		        		.concat(odobenusProperties.getApp())
		        		.concat("/version")
		        		.concat("/"+odobenusProperties.getVersion())
		        		.concat("/"+odobenusProperties.getEnv())
		        		.concat("/"+filePath));
	}
	
	private static String buildRemoteUrl(OdobenusProperties odobenusProperties, String filePath) {
		String url=odobenusProperties.getServerHost()
				.concat("download?fileName="+filePath)
				.concat("&app="+odobenusProperties.getApp())
				.concat("&version="+odobenusProperties.getVersion())
				.concat("&env="+odobenusProperties.getEnv());
		return url;
	}
	
	public static void downloadFile(OdobenusProperties odobenusProperties, String filePath) {
		String url=buildRemoteUrl(odobenusProperties, filePath);
		try {
			HttpTools.getFile(url, buildLocalFilePath(odobenusProperties, filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Properties load(OdobenusProperties odobenusProperties, String filePath) {
		try {
			Properties pro=new Properties();
			File file = new File(buildLocalFilePath(odobenusProperties, filePath));
			InputStream inStream = new FileInputStream(file);
			pro.load(inStream);
			return pro;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public static void changeFieldValue(Properties props,Object bean,Field[] declaredFields) {
		if(props!=null) {
			for (Field field : declaredFields) {
				OdobenusItem odobenusItem = field.getAnnotation(OdobenusItem.class);
				if (odobenusItem != null) {
					String[] items=odobenusItem.value().split(":");
					String itemKey=null;
					String itemDefaultVlue=null;
					if(items!=null) {
						if(items.length==1) {
							itemKey=items[0];
						}else if(items.length==2) {
							itemKey=items[0];
							itemDefaultVlue=items[1];
						}
					}
					String configerItemValue = (String) props.get(itemKey);
					if(configerItemValue==null) {
						configerItemValue=itemDefaultVlue;
					}
					try {
						setFieldData(field, bean, configerItemValue);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public static void setFieldData(Field field, Object bean, String data) throws Exception {
		// 注意这里要设置权限为true
		field.setAccessible(true);
		Class<?> type = field.getType();
		if (type.equals(String.class)) {
			field.set(bean, data);
		} else if (type.equals(Integer.class)) {
			field.set(bean, Integer.valueOf(data));
		} else if (type.equals(Long.class)) {
			field.set(bean, Long.valueOf(data));
		} else if (type.equals(Double.class)) {
			field.set(bean, Double.valueOf(data));
		} else if (type.equals(Short.class)) {
			field.set(bean, Short.valueOf(data));
		} else if (type.equals(Byte.class)) {
			field.set(bean, Byte.valueOf(data));
		} else if (type.equals(Boolean.class)) {
			field.set(bean, Boolean.valueOf(data));
		} else if (type.equals(Date.class)) {
			field.set(bean, new Date(Long.valueOf(data)));
		}
	}
}
